package com.chenjt.two_chatmemory;

import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.openai.OpenAiChatModel;

/**
 * 注释：
 *
 * @author chenjt
 * @date 2025/3/7 15:44
 */
public class FriendExample {


    public static void main(String[] args) {
        OpenAiChatModel model = OpenAiChatModel.builder()
                .apiKey("demo")
                .modelName("gpt-4o-mini")
                .build();


        MessageWindowChatMemory chatMemory = MessageWindowChatMemory.builder()
                .maxMessages(10)// 最多缓存10条数据
                .build();



        // 下面代码可以抽取成一个方法，解决了HelloWorld类中多轮对话的麻烦做法
        chatMemory.add(UserMessage.from("你好，我是小明，你是小明的好兄弟，叫小强"));
        AiMessage answer = model.generate(chatMemory.messages()).content();
        System.out.println(answer.text());
        chatMemory.add(answer);


        chatMemory.add(UserMessage.from("小明最好的朋友是谁？"));
        AiMessage answer2 = model.generate(chatMemory.messages()).content();
        System.out.println(answer2.text());
        chatMemory.add(answer2);



    }

}
